package com.dtgrid.jfinal;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.dtgrid.patch.DateJsonValueProcessor;
import com.jfinal.plugin.activerecord.Record;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class Convert {

	public static String getJsonStrFromListRecord(List<Record> li) {
		List<Map<String, Object>> exhibitDatas = listRecord2ListObject(li);
		// 转换为JSON数据格式
		JSONArray pagerJSON = JSONArray.fromObject(exhibitDatas);
		// 回写数据
		return pagerJSON.toString();
	}
	
	public static JSONArray getJsonObjFromListRecord(List<Record> li) {
		List<Map<String, Object>> exhibitDatas = listRecord2ListObject(li);
		// 转换为JSON数据格式
		JsonValueProcessor jsonProcessor = new DateJsonValueProcessor();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, jsonProcessor);
		// 转换为JSON数据格式
		JSONArray pagerJSON = JSONArray.fromObject(exhibitDatas, jsonConfig);
		// 回写数据
		return pagerJSON;
	}

	public static List<Map<String, Object>> listRecord2ListObject(List<Record> li) {
		List<Map<String, Object>> exhibitDatas = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < li.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			Record rc = li.get(i);
			String[] colNames = rc.getColumnNames();
			for (int j = 0; j < colNames.length; j++) {
				String key = String.valueOf(colNames[j]);
				if (rc.get(key) instanceof String) {
					String value = (String) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Integer) {
					Integer value = (Integer) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Double) {
					Double value = (Double) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Boolean) {
					Boolean value = (Boolean) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Timestamp) {
					Timestamp value = (Timestamp) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof BigDecimal) {
					BigDecimal value = (BigDecimal) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Long) {
					Long value = (Long) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Date) {
					Date value = (Date) rc.get(key);
					map.put(key, value);
				}
				else if (rc.get(key) == null) {
					map.put(key, "");
				}
				else {
					// 其他类型待添加
					System.out.println("error:" + rc.get(key) + ":==" + rc.get(key).getClass().getName());
				}

			}
			exhibitDatas.add(map);
		}
		return exhibitDatas;
	}
	
	public static List<Map<String, Object>> JSONArray2ListObject(net.sf.json.JSONArray ja) {
		List<Map<String, Object>> exhibitDatas = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < ja.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();

			JSONObject rc = JSONObject.fromObject(ja.get(i).toString());
			Iterator<?> it = rc.keys();
			// 遍历jsonObject数据，添加到Map对象
			while (it.hasNext()) {
				String key = String.valueOf(it.next());
				if (rc.get(key) instanceof String) {
					String value = (String) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Integer) {
					Integer value = (Integer) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Double) {
					Double value = (Double) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Boolean) {
					Boolean value = (Boolean) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Timestamp) {
					Timestamp value = (Timestamp) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof BigDecimal) {
					BigDecimal value = (BigDecimal) rc.get(key);
					map.put(key, value);
				} else if (rc.get(key) instanceof Long) {
					Long value = (Long) rc.get(key);
					map.put(key, value);
				}
				else if (rc.get(key) == null) {
					map.put(key, "");
				}
				else {
					// 其他类型待添加
					System.out.println("error:" + rc.get(key) + ":==" + rc.get(key).getClass().getName());
				}
			}
			exhibitDatas.add(map);
		}
		return exhibitDatas;
	}

	
}
